package chapter18;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/3/26 11:47
 */
public class FileLocking {
    public static void main(String[] args) throws IOException, InterruptedException {
        FileOutputStream fos = new FileOutputStream("file.txt");
        FileLock lock = fos.getChannel().tryLock();
        if (lock != null) {
            System.out.println("Locked file");
            TimeUnit.SECONDS.sleep(1);
            lock.release();
            System.out.println("release");
        }
    }
}
